package class1.s2022350143027.service.Impl;

import class1.s2022350143027.data.OrdersData;
import class1.s2022350143027.model.GoodsModel;
import class1.s2022350143027.model.OrdersModel;
import class1.s2022350143027.service.YzyOrdersService;
import class1.s2022350143027.utils.YzyDateTimeUtil;
import class1.s2022350143027.utils.YzyValidateUtil;

import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;


/**
 * 订单管理业务逻辑实现
 * @author yzy
 * @date 2024-05-02
 *
 */
public class YzyOrdersServiceImpl implements YzyOrdersService {

    protected OrdersData ordersData = new OrdersData();

    public OrdersData getOrdersData() {
        return ordersData;
    }

    public List<OrdersModel> getOrderModel(String orderId_or_customerName) {
        List<OrdersModel> order_list = new ArrayList<>();
        Iterator<OrdersModel> it_o = ordersData.getOrdersDataList().iterator();
        OrdersModel obj_o ;
        while (it_o.hasNext()) {
            obj_o = it_o.next();
            if (obj_o.getOrderId().equals(orderId_or_customerName) ||
                    obj_o.getOrderCustomerName().equals(orderId_or_customerName) ) {
                order_list.add(obj_o);
            }

        }
        return order_list;
    }
    public List<OrdersModel> getOrderModel(String startDate, String endDate) {
        List<OrdersModel> order_list = new ArrayList<>();
        Iterator<OrdersModel> it_o = ordersData.getOrdersDataList().iterator();
        OrdersModel obj_o ;
        while (it_o.hasNext()) {
            obj_o = it_o.next();
            if (obj_o.getOrderTime().toLocalDate().isAfter(LocalDate.parse(startDate)) ||
                    obj_o.getOrderTime().toLocalDate().isBefore(LocalDate.parse(endDate)) ) {
                order_list.add(obj_o);
            }

        }
        return order_list;
    }


    /**
     * 录入订单
     * @param ordersBaseModel 要保存的数据模型
     * @return boolean
     */
    public boolean save(OrdersModel ordersBaseModel) {
        ordersData.getOrdersDataList().add(ordersBaseModel);
        return true;
    }


    public boolean update(String lookOrder_id1) {
        //输入准备
        Scanner scanner = new Scanner(System.in);
        Iterator<OrdersModel> it_o = ordersData.getOrdersDataList().iterator();
        OrdersModel obj_o ;
        while (it_o.hasNext()) {
            obj_o = it_o.next();
            if (obj_o.getOrderId().equals(lookOrder_id1)){
                System.out.print("修改客户名:");
                String order_customerName = scanner.next();

                System.out.print("修改商品编号:");
                String order_goodId = scanner.next();

                System.out.print("修改商品名称:");
                String order_goodName = scanner.next();

                System.out.print("修改商品单价:");
                String order_goodPrice = scanner.next();

                System.out.print("修改购买数量:");
                String order_goodQuantity = scanner.next();

                if(!order_customerName.isEmpty() &&
                        !order_goodId.isEmpty() &&
                        !order_goodName.isEmpty() &&
                        YzyValidateUtil.checkGood_name(order_goodName) &&
                        YzyValidateUtil.checkPN(order_goodPrice) &&
                        YzyValidateUtil.checkPN(order_goodQuantity)){
                    obj_o.setOrderCustomerName(order_customerName);
                    obj_o.setOrderGoodId(order_goodId);
                    obj_o.setOrderGoodName(order_goodName);
                    obj_o.setOrderGoodPrice(Integer.parseInt(order_goodPrice));
                    obj_o.setOrderGoodQuantity(Integer.parseInt(order_goodQuantity));
                    obj_o.setOrderTotalPrice(obj_o.getTotalPrice());
                    return true;
                }
                //否则重新操作
                else {
                    System.out.println("录入商品信息错误，请重试！");
                }


            }
        }
        return false;
    }


    public boolean delete(String lookOrder_id) {
        Iterator<OrdersModel> it_o = ordersData.getOrdersDataList().iterator();
        OrdersModel obj_o ;
        while (it_o.hasNext()) {
            obj_o = it_o.next();
            if (obj_o.getOrderId().equals(lookOrder_id)){
                ordersData.getOrdersDataList().remove(obj_o);
                return true;
            }
        }
        return false;
    }

    public void show(List<OrdersModel> o_list) {
        System.out.println("====================================================订单信息================================================================");
        System.out.println("===  ==订单编号==        ===时间===       ==客户名==     ==商品名==     ==商品编号==     ==单价==     ==数量==     ==合计价格==  ===");

        Iterator<OrdersModel> it_o = o_list.iterator();
        OrdersModel obj_o ;
        while (it_o.hasNext()) {
            obj_o = it_o.next();
            System.out.printf("===      "+"%-8s %-21s   %-12s %-15s %-12s%-10s   %-10d %-10.2f ===\n",
                    obj_o.getOrderId(),
                    YzyDateTimeUtil.DateFormat(obj_o.getOrderTime(),"yyyy-MM-dd hh:mm:ss"),
                    obj_o.getOrderCustomerName(),obj_o.getOrderGoodName(),
                    obj_o.getOrderGoodId(),obj_o.getOrderGoodPrice(),
                    obj_o.getOrderGoodQuantity(),obj_o.getOrderTotalPrice());
        }
    }

    @Override
    public boolean SaveDataToTxt() {
        return false;
    }

    @Override
    public boolean SaveDataToSql() {
        return false;
    }

    @Override
    public boolean DeleteDataFromSql(GoodsModel myModel) throws SQLException {
        return false;
    }



}
